const Tool = require("../../../utils/tool.js");
const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServicePrize = require("../../../service/service_prize.js");

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  //
  data: {
    circleList: [],//圆点数组
    awardList: [],//奖品数组
    colorCircleFirst: '#FFDF2F',//圆点颜色1
    colorCircleSecond: '#FE4D32',//圆点颜色2
    colorAwardDefault: '#F5F0FC',//奖品默认颜色
    colorAwardSelect: '#ffe400',//奖品选中颜色
    indexSelect: 0,//被选中的奖品index
    isRunning: false,//是否正在抽奖
    imageAward: [ ],//奖品图片数组
    prize: null, // 抽奖对象,
    prizeItem: null // 中奖对象
  },

  methods: {
    onLoad: function () {
      const _this = this;
      /*
      * 圆点设置
      */
      var leftCircle = 7.5;
      var topCircle = 7.5;
      var circleList = [];
      for (var i = 0; i < 24; i++) {
        if (i == 0) {
          topCircle = 15;
          leftCircle = 15;
        } else if (i < 6) {
          topCircle = 7.5;
          leftCircle = leftCircle + 102.5;
        } else if (i == 6) {
          topCircle = 15
          leftCircle = 620;
        } else if (i < 12) {
          topCircle = topCircle + 94;
          leftCircle = 620;
        } else if (i == 12) {
          topCircle = 565;
          leftCircle = 620;
        } else if (i < 18) {
          topCircle = 570;
          leftCircle = leftCircle - 102.5;
        } else if (i == 18) {
          topCircle = 565;
          leftCircle = 15;
        } else if (i < 24) {
          topCircle = topCircle - 94;
          leftCircle = 7.5;
        } else {
          return
        }
        circleList.push({ topCircle: topCircle, leftCircle: leftCircle });
      }
      this.setData({
        circleList: circleList
      })
      /*
       * 圆点闪烁
       */
      setInterval(function () {
        if (_this.data.colorCircleFirst == '#FFDF2F') {
          _this.setData({
            colorCircleFirst: '#FE4D32',
            colorCircleSecond: '#FFDF2F',
          })
        } else {
          _this.setData({
            colorCircleFirst: '#FFDF2F',
            colorCircleSecond: '#FE4D32',
          })
        }
      }, 500)
      /*
       * 奖品item设置
       */
      /*
       * 获取数据
       */
      ServicePrize.get(function(data){
        // 获取数据
        if(data){
          for (let item of data.item) {
            _this.data.imageAward.push(item);
          }
          _this.setData({
            prize: data,
            prizeItem: data.winPrize
          })
        }
        //
        var awardList = [];
        //间距,怎么顺眼怎么设置吧.
        var topAward = 25;
        var leftAward = 25;
        for (var j = 0; j < 8; j++) {
          if (j == 0) {
            topAward = 25;
            leftAward = 25;
          } else if (j < 3) {
            topAward = topAward;
            //166.6666是宽.15是间距.下同
            leftAward = leftAward + 166.6666 + 15;
          } else if (j < 5) {
            leftAward = leftAward;
            //150是高,15是间距,下同
            topAward = topAward + 150 + 15;
          } else if (j < 7) {
            leftAward = leftAward - 166.6666 - 15;
            topAward = topAward;
          } else if (j < 8) {
            leftAward = leftAward;
            topAward = topAward - 150 - 15;
          }
          var imageAward = _this.data.imageAward[j];
          awardList.push({ topAward: topAward, leftAward: leftAward, imageAward: imageAward });
        }
        _this.setData({
          awardList: awardList
        })
      });
    },
    /**
     * 得到中奖位置
     * @return 1-8
     */
    _gainPrizePostion(callback) {
      const _this = this;
      ServicePrize.request({ prizeId: _this.data.prize.id}, function (data) {
        var i = 1;
        for (let item of _this.data.imageAward){
          if (item.id === data.id){
            callback(i, data)
            break;
          }
          i++;
        }
      });
    },
    /*
     * 开始游戏
     */
    startGame: function (e) {
      const _this = this;
      /**
       * 如果已经抽奖则拒绝
       */
      if (_this.data.prizeItem){
        Tool.showInfo("只能参与一次抽奖活动");
        return;
      }
      /**
       * 防止重复点击
       */
      if (_this.data.isRunning) return
      _this.setData({
        isRunning: true
      })
      /**
       * 从服务器获取中奖数据
       */
      _this._gainPrizePostion(function(position, data){
        // 运行计时器
        _this._run(position + 32, data);
      });
    },
    /*
    * 运行计时器
    */
    _run: function (prizePostion, data){
      const _this = this;
      var indexSelect = 0
      var i = 1;
      var timer = setInterval(function () {
        indexSelect = indexSelect % 8;
        _this.setData({
          indexSelect: indexSelect
        })
        //
        if (i++ >= prizePostion) {
          //去除循环
          clearInterval(timer)
          //获奖提示
          if (data.noPrizeFlag === false){
            wx.showModal({
              title: '抽奖结果',
              // content: '恭喜您获得' + (_this.data.indexSelect + 1) + "个优惠券",
              content: '恭喜您获得[' + data.name + "]",
              showCancel: false,//去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  _this.setData({
                    isRunning: false,
                    prizeItem: data
                  })
                }
              }
            })
          }else{
            wx.showModal({
              title: '抽奖结果',
              content: '遗憾未中奖，谢谢参与！',
              showCancel: false,//去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  _this.setData({
                    isRunning: false,
                    prizeItem: data
                  })
                }
              }
            })
          }
        }
        //
        indexSelect++;
      }, 200)
    }
  }

})